import {
	reactive,
	ref,
	onMounted
} from 'vue'

export default class MusicService {
	popupRef = ref(null)
	audioData = reactive({
		startTime: '00:00',
		duration: '00:00',
		playbackRate: '1.0',
		max: 1,
		value: 0,
		isPlay: false,
		isDone: false
	})

	isDragging = false

	constructor(src) {
		this.audioPlayer = uni.createInnerAudioContext()
		this.audioPlayer.src = src
		this.audioPlayer.autoplay = false
		this.addEventListeners()
	}

	// 监听
	addEventListeners() {
		this.audioPlayer.onError((error) => this.onError(error))
		this.audioPlayer.onCanplay(() => this.onCanplay())
		this.audioPlayer.onPlay(() => this.onPlay())
		this.audioPlayer.onPause(() => this.onPause())
		this.audioPlayer.onTimeUpdate(() => this.onTimeUpdate())
	}

	onError(error) {
		console.error('Audio error:', error)
	}

	onCanplay() {


		setTimeout(() => {
			this.audioData.startTime = this.formatTime(this.audioPlayer.currentTime)
			this.audioData.duration = this.formatTime(this.audioPlayer.duration)
			this.audioData.max = this.audioPlayer.duration

		}, 100)

		this.isDone = true

		// this.audioData.value = 0;
		// console.log('可以播放了，当前音频长度：', this.audioPlayer.duration)


	}

	onPlay() {
		// console.log(this);
		this.audioData.isPlay = true

		setTimeout(() => {
			this.audioData.startTime = this.formatTime(this.audioPlayer.currentTime)
			this.audioData.duration = this.formatTime(this.audioPlayer.duration)
			this.audioData.max = this.audioPlayer.duration
		}, 100)

	}

	onPause() {
		this.audioData.isPlay = false
	}

	onTimeUpdate() {
		if (this.isDragging) return
		this.audioData.value = this.audioPlayer.currentTime
		this.audioData.startTime = this.formatTime(this.audioPlayer.currentTime)
	}

	// 转化为分钟数
	formatTime(seconds) {
		if (seconds < 0) {
			return '00:00'
		}

		// 计算分钟和剩余秒数
		const minutes = Math.floor(seconds / 60)
		const remainingSeconds = Math.floor(seconds % 60)

		// 格式化为 00:00 形式
		const formattedMinutes = String(minutes).padStart(2, '0')
		const formattedSeconds = String(remainingSeconds).padStart(2, '0')

		return `${formattedMinutes}:${formattedSeconds}`
	}

	play = () => {
		this.audioPlayer.play()

		setTimeout(() => {
			this.audioData.startTime = this.formatTime(this.audioPlayer.currentTime)
			this.audioData.duration = this.formatTime(this.audioPlayer.duration)
			this.audioData.max = this.audioPlayer.duration
		}, 100)

		// console.log(this.audioData, 2222);
		// callback(this.audioData)
	}

	pause = () => {
		this.audioPlayer.pause()
	}

	stop = () => {
		this.audioPlayer.stop()
		this.audioPlayer.seek(0)
		this.audioData.startTime = this.formatTime(0)
	}

	seek = (seek) => {
		if (this.audioPlayer.currentTime + seek > this.audioPlayer.duration) {
			this.audioPlayer.seek(this.audioPlayer.duration)
			this.audioData.startTime = this.formatTime(this.audioPlayer.duration)
		} else if (this.audioPlayer.currentTime + seek < 0) {
			this.audioPlayer.seek(0)
			this.audioData.startTime = this.formatTime(0)
		} else {
			this.audioPlayer.seek(this.audioPlayer.currentTime + seek)
			this.audioData.startTime = this.formatTime(this.audioPlayer.currentTime + seek)
		}
	}

	playbackRate = (playbackRate) => {
		this.audioData.playbackRate = playbackRate
		this.popupRef.value.close()
		this.audioPlayer.playbackRate = +playbackRate
		
		this.pause();
		this.play();
	}

	//销毁innerAudioContext()实例
	destroy = () => {
		if (this.audioPlayer) {
			this.audioPlayer.destroy()
		}
	}

	// 拖动
	sliderChanging = ({
		detail
	}) => {
		if (!this.isDragging) this.isDragging = true
		this.audioData.startTime = this.formatTime(detail.value)
	}

	sliderChange = ({
		detail
	}) => {
		console.log('🚀 ~ MusicService ~ detail:', detail)
		this.isDragging = false
		this.audioPlayer.seek(detail.value)
	}
}